package com.dao;

import com.entity.UserEntity;
import top.appx.easysql.BaseDatabase;
import top.appx.easysql.Restrain;
import top.appx.easysql.data.DataRow;
import top.appx.easysql.data.PageInfo;
import top.appx.espring.annotation.Source;
import top.appx.espring.vo.QueryPageVO;
import top.appx.zutil.DateTime;

import java.util.List;


@Source
public class UserDao extends BaseDao<UserEntity> {
    public UserEntity queryByUsername(String username){
        return this.queryFirstOrNull(Restrain.eq("username",username),Restrain.eq("status","normal"));
    }
    public void updateLastTime(int id){
        execute("update "+tableName+" set lastLoginTime=? where id="+id, DateTime.now());
    }

    public PageInfo<DataRow> queryPageVO(QueryPageVO queryPageVO){
        BaseDatabase db = getDatabase();
        try{
            List<Restrain> restrains = queryPageVO.getRestrainList();

            String sql = "SELECT u.*,ug.name AS userGroupName FROM user u LEFT JOIN userGroup ug ON ug.id = u.userGroupId";
            return db.queryPageBySqlRestrain(sql,new PageInfo(queryPageVO.getPage(),queryPageVO.getPageSize()),restrains.toArray(new Restrain[restrains.size()]));
        }finally {
            db.close();
        }
    }
}
